package com.soulgame.notification;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;

/**
 * 发送通知类
 * */
public class NotifyUtil implements INotifiContants {
	private static NotificationManager mNotificationManager = null;
	private static NotificationCompat.Builder mBuilder = null;
	private static int mIndex = 1;

	public static void sendDelayNotify(Context context, NotifyInfo pNotifyInfo) {
		AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
		//
		Intent intent = new Intent(context, NotifyReceiver.class);
		intent.setAction(ACTION_NOTIFY);
		intent.putExtra(TITLE, pNotifyInfo.getTitle());
		intent.putExtra(TEXT, pNotifyInfo.getText());
		intent.putExtra(INDEX, mIndex);
		intent.putExtra(ICON, pNotifyInfo.getIconId());
		
		System.out.println("启动类："+pNotifyInfo.getOpenClass().getName());
		
		if (pNotifyInfo.getOpenClass() != null) {
			intent.putExtra(OPEN_ACTIVITY_NAME, pNotifyInfo.getOpenClass().getName());
		} else {
			intent.putExtra(OPEN_ACTIVITY_NAME, "");
		}
		//
		long currentTimeMillis = System.currentTimeMillis();
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(currentTimeMillis);
		calendar.add(Calendar.DATE, pNotifyInfo.getDelayDay());
		calendar.add(Calendar.HOUR, pNotifyInfo.getDelayHour());
		calendar.add(Calendar.MINUTE, pNotifyInfo.getDelayMinute());
		calendar.add(Calendar.SECOND, pNotifyInfo.getDelaySecond());
		//
		long timeInMillis = calendar.getTimeInMillis();
		long delayTimes = timeInMillis - currentTimeMillis;
		//
		System.out.println("延迟 " + delayTimes + " 毫秒Notify");
		//
		SimpleDateFormat sFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//		Calendar pCalendar = Calendar.getInstance();
//		pCalendar.setTimeInMillis(delayTimes);
		String tiem = sFormat.format(new Date(delayTimes));
		//
		System.out.println("延迟 " + tiem + "Notify");
		//
		PendingIntent pendingIntent = PendingIntent.getBroadcast(context, mIndex, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		//AlarmManager.RTC_WAKEUP表示闹钟在睡眠状态下会唤醒系统并执行提示功能，该状态下闹钟使用绝对时间，状态值为0；
		manager.set(AlarmManager.RTC_WAKEUP, timeInMillis, pendingIntent);

		mIndex++;
	}

	/**
	 * 获取通知索引，一般没什么用，仅用于调试
	 * */
	public static int getIndex() {
		return mIndex;
	}

	/**
	 * 发送通知
	 * 
	 * @param context
	 * @param index
	 * @param title
	 * @param text
	 * @param icon
	 * @param strClassName
	 */
	public static void sendNotify(Context context, int index, String title, String text, int icon, String strClassName) {
		if (mNotificationManager == null) {
			mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
		}

		mBuilder = new NotificationCompat.Builder(context);
		mBuilder.setContentIntent(getDefalutIntent(context, Notification.FLAG_AUTO_CANCEL)).setWhen(System.currentTimeMillis())
				.setPriority(Notification.PRIORITY_DEFAULT).setOngoing(false).setDefaults(Notification.DEFAULT_VIBRATE)
				.setSmallIcon(icon);

		mBuilder.setAutoCancel(true).setContentTitle(title).setContentText(text);

		Class<?> classInfo = null;
		try {
			classInfo = Class.forName(strClassName);
		} catch (Exception e) {
			e.getCause().printStackTrace();
		}

		if (classInfo != null) {
			Intent resultIntent = new Intent(context, classInfo);
			resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
			PendingIntent pendingIntent = PendingIntent.getActivity(context, index, resultIntent,
					PendingIntent.FLAG_UPDATE_CURRENT);
			mBuilder.setContentIntent(pendingIntent);
		}
		mNotificationManager.notify(index, mBuilder.build());
	}

	private static PendingIntent getDefalutIntent(Context context, int flags) {
		PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, new Intent(), flags);
		return pendingIntent;
	}
}
